/**
 * Copyright (C) 2014-2015 5WeHealth Technologies. All rights reserved.
 *  
 *    @author: Jingtao Yun Jan 15, 2015
 */
package com.wehealth.electrocardiograph_two_client.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.wehealth.electrocardiograph_two_client.ClientApp;


public class DbOpenHelper extends SQLiteOpenHelper{

	private static final int DATABASE_VERSION = 1;
	private static DbOpenHelper instance;

	private static final String PATIENT_USER_TABLE_CREATE = "CREATE TABLE "
			+ PatientUserDao.TABLE_NAME+" ("
			+ PatientUserDao.COLUMN_NAME_CARD_ID+" TEXT PRIMARY KEY , "
			+ PatientUserDao.COLUMN_NAME_NAME+" TEXT, "
			+ PatientUserDao.COLUMN_NAME_AGE+" INTEGER, "
			+ PatientUserDao.COLUMN_NAME_GENDER+" INTEGER, "
			+ PatientUserDao.COLUMN_NAME_UPDATE_TIME+" TEXT);";

	DbOpenHelper(Context context, String serialNo) {
		super(context, getECGDatabaseName(context, serialNo), null, DATABASE_VERSION);
	}
	
	public static DbOpenHelper getInstance() {
		if (instance == null) {
			instance = new DbOpenHelper(ClientApp.getInstance(), "ecg_check_th3");
		}
		return instance;
	}
	
	private static String getECGDatabaseName(Context cxt, String serialNo) {
        return  serialNo + ".db";
    }
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(PATIENT_USER_TABLE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}
	
	public void closeDB() {
	    if (instance != null) {
	        try {
	            SQLiteDatabase db = instance.getWritableDatabase();
	            db.close();
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        instance = null;
	    }
	}
}
